/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.kamai.webservice.client;

import com.edu.kamai.client.control.contestControl;
import com.edu.kamai.client.control.paperControl;
import com.edu.kamai.webservice.contest.Contest;
import com.edu.kamai.webservice.contest.ContestService;
import com.edu.kamai.webservice.contest.ContestService_Service;
import java.net.URL;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.ws.soap.SOAPFaultException;

/**
 *
 * @author xyzhu
 */
public class WSContestClient {
    private final QName qName = new QName("http://webservice.kamai.edu.com/",
            "contestService");
    private URL[] url;
    private int size;
    
    public WSContestClient() throws Exception{
        size = paperControl.getNetList().size();
        url = new URL[size];
        for(int i=0;i<size;i++){
            url[i] = new URL("http://" + contestControl.getNetList().get(i) + 
                    "/contestService/contestService?WSDL");
        }
    }
    
    public int addContest(String cTitle,String cDesc,String uID,int rID,
            int sID,int wID,int lID) throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        int res = 0;
        
        do{
            try{
                System.out.println("addContest : " + url[k]);
                ContestService_Service service = new ContestService_Service(url[k], 
                        qName);
                ContestService port = service.getContestServicePort();
                res = port.addContest(cTitle, cDesc, uID, rID, lID, sID, wID);
                find = true;
                break;
            }catch(SOAPFaultException e1){
                e1.printStackTrace();
                ex = e1;
                break;
            }catch(Exception e){
               e.printStackTrace();
               ex = e;
               k=(k+1)%size;
               break;
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return res;
        
    }
    
    public List<Contest> findContest() throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        List<Contest> retContest = null;
        do{
            try{
                System.out.println("findContest : " + url[k]);
                ContestService_Service service = new ContestService_Service(url[k], 
                        qName);
                ContestService port = service.getContestServicePort();
                //retContest = port.findContest();
                
                retContest = port.findContest();
                //if(retContest != null){
                find = true;
                //}
                break;
            }catch(Exception e){
                e.printStackTrace();
                ex = e;
                k=(k+1)%size;
                break;
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return retContest;
    }
    
    
    public Contest findContestById(int cid) throws Exception{
        int ran = (int) (size * Math.random());
        int k = ran;
        boolean find = false;
        Exception ex = null;
        Contest retContest = null;
        do{
            try{
                System.out.println("findContest by ID: " + url[k]);
                ContestService_Service service = new ContestService_Service(url[k], 
                        qName);
                ContestService port = service.getContestServicePort();
                retContest = port.findContestByID(cid);
                find = true;
                break;
            }catch(Exception e){
                e.printStackTrace();
                ex = e;
                k=(k+1)%size;
                break;
            }
        }while(k!=ran);
        if(!find){
            throw ex;
        }
        return retContest;
    }
    
}
